package com.quanquan.management.controller;

import DTO.PageDTO;
import com.quanquan.management.dto.IntegralTradingDetailsVO;
import com.quanquan.management.request.IntegralDetailsRequestBody;
import com.quanquan.management.service.IntegralTradingService;
import enums.ResCodeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import web.response.ResultBody;

/**
 * 功能描述:会员积分明细管理
 *
 * @ClassName: IntegralTradingController
 * @Author: xiaoqianh丶
 * @Date: 2019-03-29 14:06
 * @Version: V1.0
 */

@Controller
@RequestMapping("/integral")
public class IntegralTradingController extends ManagerBaseController {

    @Autowired
    private IntegralTradingService integralTradingService;

    @GetMapping("/list")
    public String list(@RequestParam(required = false) Integer pageSize,
                       @RequestParam(required = false) Integer page,
                       @RequestParam(required = false) String fromDate,
                       @RequestParam(required = false) String toDate,
                       @RequestParam(required = false) String storeName,
                       @RequestParam(required = false) String consumeType,
                       @RequestParam(required = false) String keyword, Model model) {
        if (null == pageSize) {
            pageSize = 10;
        }
        if (null == page) {
            page = 0;
        } else if (page > 0){
            page = page -1;
        }

        IntegralDetailsRequestBody request = new IntegralDetailsRequestBody();
        request.setStoreName(storeName);
        request.setPage(page);
        request.setSize(pageSize);
        request.setKeyword(keyword);
        request.setFromDate(fromDate);
        request.setToDate(toDate);
        request.setConsumeType(consumeType);
        PageDTO<IntegralTradingDetailsVO> pageDTO = integralTradingService.listIntegralTrading(request);
        model.addAttribute("pageObj", pageDTO);
        model.addAttribute("consumeType", consumeType);
        return "integralTrading/list";
    }

    @RequestMapping("/export")
    public ResultBody exportData(
                                 @RequestParam(required = false) String keyword,
                                 @RequestParam(required = false) String fromDate,
                                 @RequestParam(required = false) String toDate,
                                 @RequestParam(required = false) String storeName,
                                 @RequestParam(required = false) String consumeType,
                                 @RequestParam(required = false) Integer total
    ) {

        IntegralDetailsRequestBody request = new IntegralDetailsRequestBody();
        request.setPage(0);
        request.setSize(total);
        request.setKeyword(keyword);
        request.setFromDate(fromDate);
        request.setToDate(toDate);
        request.setStoreName(storeName);
        request.setFromDate(fromDate);
        request.setToDate(toDate);
        request.setConsumeType(consumeType);

        try{
            integralTradingService.export(this.response,request);
            return new ResultBody(ResCodeEnum.SUCCESS.getCode(),ResCodeEnum.SUCCESS.getMsg());
        }catch (Exception e){
            e.printStackTrace();
            return new ResultBody(ResCodeEnum.FAIL.getCode(),ResCodeEnum.FAIL.getMsg());
        }
    }

}
